var zongjia;
function Modifyaddress(ress_id, obj) {//选择邮寄地址
    var dede = {address_id: ress_id};
    d = Aajax(homeUrl + "/Trader/setDefaultAddress", dede);
    if (d) {
        obj.addClass("curr").siblings().removeClass("curr");
        $("#confirmAddr_1").html(Aajaxobj(homeUrl + "/Trader/defaultAdderss"));
        if(detype){
            var date = Aajaxobj(homeUrl + "/Trader/reloadCartBuy",dedede);
            $("#inventory").html(date['html']);
            $("#anonymous_1").html(date['voucher']);
            $("#anonymous_2").html(date['mansong']);
            $(".realPay-price").html(date['totalPrice']);
        }else{
            var date =Aajaxobj(homeUrl + "/Trader/reloadGoods",dedede);
            $("#inventory").html(date['html']);
            $("#anonymous_1").html(date['voucher']);
            $("#anonymous_2").html(date['mansong']);
            $(".realPay-price").html(date['totalPrice']);
        }
    } else {
        //失败
    }
}


function AddressEdit(ress_id) { //修改地址信息
    var d = null;
    d = Aajax(homeUrl + "/Trader/address", ress_id)
    $("#popDiv").html(d);
    $("#popDiv").show();
}
function RemoveressEdit(ress_id) { //删除地址
    var d = null;
    d = Aajax("url", ress_id);
    if (d) {
        $("#Personal_address").html(Aajaxobj(homeUrl + "/Trader/reloadAddress"));
        $("#inventory").html(Aajaxobj("url", "inventory"))
    }
}
function closePopDiv() {
    $("#popDiv").hide();
}
function submitAddress() { //添加 修改地址信息
    var d = null;
    if (!currress()) {
        return;
    }
    var p = submitaddajax();
    var d = null;
    if (p) {
        d = Aajax(homeUrl + "/Trader/saveAddress", p);
    }
    if (d) {
        $("#Personal_address").html(Aajaxobj(homeUrl + "/Trader/reloadAddress"));
        $a = Aajaxobj(homeUrl + "/Trader/reloadGoods",dedede);
        $("#inventory").html($a['html']);
        $("#confirmAddr_1").html(Aajaxobj(homeUrl + "/Trader/defaultAdderss"));
    }
}
function currress() {
    if (confirm("修改地址将重置部分信息？"))
    {
        return true;
    } else
    {
        return false;
    }
}
function Distribution(obj) {
    var objtitt = obj.attr("title");
    var objid = obj.attr("id");
    if (Aajax("url", objid)) {
        $("#desc_916").text(objtitt);
        $("#pay_ship_916").val(objid);
        $("#pay_ship_916").attr("goods_postage", obj.attr("goods_postage"));
        obj.addClass("shipping_type_curr").siblings().removeClass("shipping_type_curr")
    }
}
function goods_as(_this) {
    var obj = _this.siblings("input");
    var num = obj.val();
    var dete = null;
    var stock=Number(obj.attr("stock"));
    dete = obj.attr("goods_id");
    var guige1 = obj.attr('guige1');
    var guige2 = obj.attr('guige2');
    if (_this.hasClass("jian")) {
        if (num == 1) {
            return;
        } else {
            dete = {goods_num: --num, goods_id: dete, guige1: guige1, guige2: guige2};
        }
    } else {
        if(stock==num){
            layer.msg("库存不足")
            return;
        }else{
            dete = {goods_num: ++num, goods_id: dete, guige1: guige1, guige2: guige2};
        }
    }
    //d.p修改状态true/false  d.m 小计  d.s修改失败的原因
    var date = Aajax(homeUrl + "/Trader/reloadGoods", dete);
    $("#inventory").html(date['html']);
    $("#anonymous_1").html(date['voucher']);
    $("#anonymous_2").html(date['mansong']);
    $(".realPay-price").html(date['totalPrice']);
    $(".realPay-price").val(date['totalPrice']);
    zongjia=date['totalPrice'];
}
function goods_cp_dp(obj) {
    obj.find("option").each(function () {
        if ($(this).attr("selected")) {
            var num = Number($(this).attr("goods_postage"));
            var _thistd = $(this).parents("td");
            _thistd.find(".main-color").find("b").text((Number(_thistd.find(".dp_price_val").val()) - num).toFixed(2));
        }
    })
    zong();
}
function zong() {
    var num=0;
    $(".pt_cp1").each(function(){
        $(this).find("option").each(function(){
            if ($(this).attr("selected")) {
                num += Number($(this).attr("goods_postage"));
            }
        })
    })
    var prick=Number($("input.realPay-price").val())-num
    $("span.realPay-price").text(prick.toFixed(2));
}

function showCheckoutOther(obj) {
    var otherParent = obj.parentNode;
    otherParent.className = (otherParent.className == 'checkout_other') ? 'checkout_other2' : 'checkout_other';
    var spanzi = obj.getElementsByTagName('span')[0];
    spanzi.innerHTML = spanzi.innerHTML == '+' ? '-' : '+';
}
function ordersubmit(){
//	alert("通知：因春节快递放假，2017年1月23日-2月4日淘嘟嘟网站的产品，只接单，不发货，预计发货时间2017年2月5日。客服电话：400-0371-775 ");
    var pan_duan=$("#Personal_address > ul > li.defaultAddr").length;
    var num_peisong=$("#noExpree_input").length;
    if(pan_duan&&!num_peisong){
        return true;
    }else{
        layer.open({content:"您未选择邮寄地址或该地区不支持配送"});
        return false;
    }
}
function submitaddajax() {  //验证
    if ($("#region > select").eq(1).val()>0) $('#city_id').val($("#region > select").eq(1).val());
    var frm = document.forms['theForm'];
    var cmt = new Object;
    cmt.consignee = frm.elements['consignee'].value; //姓名
    cmt.address = frm.elements['address'].value;   //街道
    cmt.email = frm.elements['email'].value;     //邮箱
    cmt.zipcode = frm.elements['zipcode'].value; //邮编
    cmt.city_id = frm.elements['city_id'].value; //省
    cmt.area_id = frm.elements['area_id'].value;      //市
    cmt.area_info = frm.elements['area_info'].value; //县 下一条电话
    cmt.tel = frm.elements['tel_01'].value + "-" + frm.elements['tel_02'].value + (Utils.isEmpty(frm.elements['tel_03'].value) ? "" : "-" + frm.elements['tel_03'].value);
    cmt.mobile = frm.elements['mobile'].value; //手机号
    //console.log(cmt)
    if (cmt.consignee.length == 0) {
        $('form[name=theForm]').find('.reciever-notice').html('收件人不能为空').parents('.required-notice').css('visibility', 'visible');
        //alert('收件人不能为空！');
        return false;
    } else {
        $('form[name=theForm]').find('.reciever-notice').html('').parents('.required-notice').css('visibility', 'hidden');
    }
    if (cmt.optionlength > 1) {
        if (cmt.city_id == '0' || cmt.area_id == '0' || cmt.area_info == '0') {
            $('form[name=theForm]').find('.address-notice').html('所在地区不完整').parents('.required-notice').css('visibility', 'visible');
            //alert('所在地区不完整！');
            return false;
        } else {
            $('form[name=theForm]').find('.address-notice').html('').parents('.required-notice').css('visibility', 'hidden');
        }
    } else {
        if (cmt.city_id == '0' || cmt.area_id == '0') {
            $('form[name=theForm]').find('.address-notice').html('所在地区不完整').parents('.required-notice').css('visibility', 'visible');
            //alert('所在地区不完整！');
            return false;
        } else {
            $('form[name=theForm]').find('.address-notice').html('').parents('.required-notice').css('visibility', 'hidden');
        }

    }
    if (cmt.email.length > 0 && (!Utils.isEmail(cmt.email))) {
        alert('邮箱地址不正确');
        return false;
    }
    if (cmt.address.length == 0) {
        $('form[name=theForm]').find('.detail-address-notice').html('街道地址不能为空').parents('.required-notice').css('visibility', 'visible');
        //alert('街道地址不能为空！');
        return false;
    } else {
        $('form[name=theForm]').find('.detail-address-notice').html('').parents('.required-notice').css('visibility', 'hidden');
    }

    if (cmt.mobile.length == 0 && (cmt.tel.length == 0 || cmt.tel == '--' || cmt.tel == '-')) {
        $('form[name=theForm]').find('.phone-notice').html('手机号码和固定电话必须填写至少一项').parents('.required-notice').css('visibility', 'visible');
        //alert('手机号码和固定电话必须填写至少一项！');
        return false;
    } else {
        $('form[name=theForm]').find('.phone-notice').html('').parents('.required-notice').css('visibility', 'hidden');
    }
    if (cmt.mobile.length > 0) {
        var reg = /^1[34578][0-9]\d{8}$/;
        if (!Utils.isMobile(cmt.mobile)) {
            $('form[name=theForm]').find('.phone-notice').html('手机号码格式不正确').parents('.required-notice').css('visibility', 'visible');
            //alert('手机号码格式不正确！');
            return false;
        } else {
            $('form[name=theForm]').find('.phone-notice').html('').parents('.required-notice').css('visibility', 'hidden');
        }
    }
    if (cmt.tel.length > 0 && cmt.tel != '--' && cmt.tel != '-') {
        var reg = /^[\d|\-|\s]+$/;
        if (!Utils.isTel(cmt.tel)) {
            $('form[name=theForm]').find('.phone-notice').html('固定电话格式不正确').parents('.required-notice').css('visibility', 'visible');
            //alert('固定电话格式不正确！');
            return false;
        } else {
            $('form[name=theForm]').find('.phone-notice').html('').parents('.required-notice').css('visibility', 'hidden');
        }
    }
    return cmt;
}
